/**
 * 
 */
package com.appspot.android2gather.persistance;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;

import com.appspot.android2gather.activity.Activity;
import com.beoui.geocell.model.LocationCapable;
import com.beoui.geocell.model.Point;
import com.google.appengine.api.datastore.Category;
import com.google.appengine.api.users.User;

/**
 * @author Stefan Thaler
 */
@PersistenceCapable
public class ActivityEntity
    implements LocationCapable, Activity
{
    @PrimaryKey
    @Persistent( valueStrategy = IdGeneratorStrategy.IDENTITY )
    @Getter( AccessLevel.PUBLIC )
    private Long id;

    @Persistent
    @Getter( AccessLevel.PUBLIC )
    @Setter( AccessLevel.PUBLIC )
    private Double latitude;

    @Persistent
    @Getter( AccessLevel.PUBLIC )
    @Setter( AccessLevel.PUBLIC )
    private Double longitude;

    @Persistent
    @Getter( AccessLevel.PUBLIC )
    @Setter( AccessLevel.PUBLIC )
    private Double altitude;

    @Persistent
    @Getter( AccessLevel.PUBLIC )
    @Setter( AccessLevel.PUBLIC )
    private transient List<String> geocells;

    @Persistent
    @Getter( AccessLevel.PUBLIC )
    @Setter( AccessLevel.PUBLIC )
    private String name;
    

    @Persistent
    @Getter( AccessLevel.PUBLIC )
    @Setter( AccessLevel.PUBLIC )
    private String facebookEventId;

    @Persistent
    @Getter( AccessLevel.PUBLIC )
    @Setter( AccessLevel.PUBLIC )
    private String description;

    @Persistent
    @Getter( AccessLevel.PUBLIC )
    @Setter( AccessLevel.PUBLIC )
    private Long startTime;

    @Persistent
    @Getter( AccessLevel.PUBLIC )
    @Setter( AccessLevel.PUBLIC )
    private Long endTime;

    @Persistent
    @Getter( AccessLevel.PUBLIC )
    private ArrayList<Category> categories;

    @Persistent
    @Getter( AccessLevel.PUBLIC )
    @Setter( AccessLevel.PUBLIC )
    private ArrayList<User> attendees = new ArrayList<User>();

    @Persistent
    @Getter( AccessLevel.PUBLIC )
    private User creator;

    @Override
    public String getKeyString()
    {

        return Long.valueOf( getId() ).toString();
    }

    @Override
    public Point getLocation()
    {
        return new Point( getLatitude(), getLongitude() );
    }

    public void setCreator( User u )
    {
        if ( null != u )
        {
            this.creator = u;
        }
        else
        {
            throw new NullPointerException( "Creator must not be null" );
        }
    }

    public void addAttendee( User u )
    {
        if ( null != u )
        {
            int i = 0;
            for ( User a : getAttendees() )
            {
                if ( u.getEmail().equals( a.getEmail() ) )
                {
                    getAttendees().set( i, u ); // update if existing
                    return;
                }
                i++;
            }
            // no user like that, add
            getAttendees().add( u );
        }
        else
        {
            throw new NullPointerException( "Attendee must not be null" );
        }
    }

    public boolean removeAttendee( User u )
    {
        if (u.getEmail().equals( getCreator().getEmail() )) {
            return false;
        }
        if ( null != u )
        {
            int i = 0;
            for ( User a : getAttendees() )
            {
                if ( u.getEmail().equals( a.getEmail() ) )
                {
                    getAttendees().remove( i ); // remove if existing
                    return true;
                }
                i++;
            }
            return false;
        }
        else
        {
            throw new NullPointerException( "Attendee must not be null" );
        }
    }

    public void setCategories( String commaSeperatedCategories )
    {
        StringTokenizer st = new StringTokenizer( commaSeperatedCategories, "," );
        ArrayList<Category> categories = new ArrayList<Category>();

        while ( st.hasMoreTokens() )
        {
            categories.add( new Category( st.nextToken() ) );
        }

        setCategories( categories );
    }

    public void setCategories( ArrayList<Category> categories2 )
    {
        this.categories = categories2;

    }

    public Integer getNumberOfAttendees()
    {
        return getAttendees().size();
    }


    public static void main(String[] args)
    {
        System.out.println(System.currentTimeMillis());
    }
}
